package com.example.tvfoucs;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.owen.focus.AbsFocusBorder;
import com.owen.focus.FocusBorder;

public class MainActivity extends AppCompatActivity {
    /** 颜色焦点框 */
    private FocusBorder mColorFocusBorder;
    /** 图片焦点框 */
    private FocusBorder mDrawableFocusBorder;
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_photo);
        relativeLayout = findViewById(R.id.rlFigure1);
        initBorder();
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"1111111",Toast.LENGTH_LONG).show();
            }
        });
        relativeLayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Toast.makeText(MainActivity.this,"222222",Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initBorder() {
        /** 颜色焦点框 */
        mColorFocusBorder = new FocusBorder.Builder().asColor()
                //阴影宽度(方法shadowWidth(18f)也可以设置阴影宽度)
                .shadowWidth(TypedValue.COMPLEX_UNIT_DIP, 5f)
                //阴影颜色
                .shadowColor(getResources().getColor(R.color.aliceblue))
                //边框宽度(方法borderWidth(2f)也可以设置边框宽度)
                .borderWidth(TypedValue.COMPLEX_UNIT_DIP, 3.5f)
                //边框颜色
                .borderColor(getResources().getColor(R.color.paleturquoise))
                //padding值
//                .padding(10f)
                //动画时长
                .animDuration(300)
                //不要闪光效果动画
//                .noShimmer()
                //闪光颜色
                .shimmerColor(getResources().getColor(R.color.color_66FFFFFF))
                //闪光动画时长
                .shimmerDuration(1000)
                //不要呼吸灯效果
//                .noBreathing()
                //呼吸灯效果时长
                .breathingDuration(1000)
                //边框动画模式
                .animMode(AbsFocusBorder.Mode.SEQUENTIALLY)
                .build(this);

        /** 图片焦点框 */
        mDrawableFocusBorder = new FocusBorder.Builder().asDrawable()
                .borderDrawableRes(R.drawable.focus)
                //标题相关配置
//                .titleBackgroundRes(R.drawable.shape_item_title_bg)
                .titleMarginBottomAutoAlignBorder()
                .titleMargin(10, 0, 10, 4)
//                .titlePadding(10)
                .titleWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                .titleTextColor(Color.LTGRAY)
                .titleTextSize(24)
                //边框动画模式
                .animMode(AbsFocusBorder.Mode.SEQUENTIALLY)
                .build(this);

        //焦点监听 方式一:绑定整个页面的焦点监听事件
        mColorFocusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback() {
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus) {
                if(null != newFocus) {
                    switch (newFocus.getId()) {
                        case R.id.rlFigure1:
                        case R.id.rlFigure2:
                        case R.id.llFigureMore:

                        case R.id.llAllImage:
                        case R.id.rlTime1:
                        case R.id.rlTime2:
                        case R.id.llTimeMore:
                        case R.id.llElePhoto:
                        case R.id.llCloud:

                        case R.id.rlAddress1:
                        case R.id.rlAddress2:
                        case R.id.llAddressMore:
                            return  createColorBorderOptions(0);

//                            return  createColorBorderOptions(20);

//                        case R.id.llTimeMore:
//                        case R.id.llElePhoto:
//                        case R.id.llCloud:
//                            return   createColorBorderOptions(30);

//                        case R.id.rlAddress1:
//                        case R.id.rlAddress2:
//                        case R.id.llAddressMore:
//
//                            return  createColorBorderOptions(60);

//                        case R.id.round_frame_layout_7:
//                            return createColorBorderOptions(20);
//                        case R.id.round_frame_layout_3:
//                        case R.id.round_frame_layout_8:
//                            return createColorBorderOptions(40);
//                        case R.id.round_frame_layout_4:
//                        case R.id.round_frame_layout_9:
//                            return createColorBorderOptions(60);
//                        case R.id.round_frame_layout_5:
//                        case R.id.round_frame_layout_10:
//                            return createColorBorderOptions(90);

                        default:
                            break;
                    }
                }
                mColorFocusBorder.setVisible(false);
                //返回null表示不使用焦点框框架
                return null;
            }
        });


        /** 图片焦点框 */
        mDrawableFocusBorder = new FocusBorder.Builder().asDrawable()
                .borderDrawableRes(R.drawable.focus)
                //标题相关配置
//                .titleBackgroundRes(R.drawable.shape_item_title_bg)
//                .titleMarginBottomAutoAlignBorder()
//                .titleMargin(10, 0, 10, 4)
//                .titlePadding(10)
//                .titleWidth(ViewGroup.LayoutParams.MATCH_PARENT)
//                .titleTextColor(Color.LTGRAY)
//                .titleTextSize(24)
                //边框动画模式
                .animMode(AbsFocusBorder.Mode.SEQUENTIALLY)
                .build(this);

        //焦点监听 方式二:绑定单独控件的焦点监听事件
//                mDrawableFocusBorder.onFocus(itemView, FocusBorder.OptionsFactory.get(1.2f, 1.2f, "标题文本 " + position));

        mDrawableFocusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback() {
            @Nullable
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus) {
               switch (newFocus.getId()){
//                   case R.id.rlAddress1:
//                   case R.id.rlAddress2:
//                   case R.id.llAddressMore:
//                       return  createDrawableFocusBorder(0);
               }



                return null;
            }
        });

    }

    private FocusBorder.Options createColorBorderOptions(int radius) {
        mDrawableFocusBorder.setVisible(false);
        float scale = 1.05f;
        return FocusBorder.OptionsFactory.get(scale, scale, dp2px(radius) * scale);
    }
    private FocusBorder.Options createDrawableFocusBorder(int radius) {
        mColorFocusBorder.setVisible(false);
        float scale = 1.1f;
        return FocusBorder.OptionsFactory.get(scale, scale, dp2px(radius) * scale);
    }

    private float dp2px(int dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

}
